/////////////////////////////////////////////////////////////
// File Name	:	"CGame.cpp"
//
// Author Name	:	Brandon Layton (BL)
//
// Purpose:		:	To encapulsate all game related code
/////////////////////////////////////////////////////////////
#include "../headers/Game.h"
//#include "CMainMenuState.h"
//#include "CTutorialState.h"
//#include "COptionsState.h"
//#include "CPauseState.h"
//#include "CSkillState.h"
//#include "CLoadingState.h"
//#include "CCreditsState.h"
//#include "CHUDState.h"
//#include "CCharacterSelectState.h"
//#include "CMapSelectState.h"
//#include "CIntroState.h"
//#include "CSaveLoadState.h"
#include "../headers/GameplayState.h"
#include "../Messaging/MessageSystem.h"
#include "../Messaging/EventSystem.h"

#include "../headers/ObjectManager.h"

#include "../tools/MathHelper.h"
#include <ctime>

//constructor
CGame::CGame()
{
	m_pD3D	= NULL;
	m_pTM	= NULL;
	//m_pFM	= NULL;
	m_pInput	= NULL;
	m_pMS	= NULL;
	m_pES	= NULL;
	m_pOM	= NULL;
	m_pBMF	= NULL;
	m_pAS	= NULL;

	m_fSFXVolume = 1.0f;
	m_fMusicVolume = 1.0f;
	m_fGlobalVolume = 1.0f;
	m_fPanning = 0;
}

//destructor
CGame::~CGame()
{

}


//Singleton accessor
CGame * CGame::GetInstance(void)
{
	static CGame game;

	return &game;
}

//Initialization
void CGame::Initialization(HWND hWnd, HINSTANCE hInstance, int nScreenWidth, int nScreenHeight, bool bIsWindowed)
{
	m_nScreenHeight = nScreenHeight;
	m_nScreenWidth = nScreenWidth;
	m_nPrevScrH = 0;
	m_nPrevScrW = 0;
	SetGlobalScaleX();
	SetGlobalScaleY();

	//Get pointers to singletons
	m_pD3D	= CDirect3D::GetInstance();
	m_pTM	= CTextureManager::GetInstance();
	//m_pFM	= CFModManager::GetInstance();
	m_pInput = CInput::GetInstance();
	m_pMS	= CMessageSystem::GetInstance();
	m_pES	= CEventSystem::GetInstance();
	m_pOM	= CObjectManager::GetInstance();
	m_pAS	= CAnimationSystem::GetInstance();
	
	//initialize singletons:
	m_pD3D->InitDirect3D(hWnd, nScreenWidth, nScreenHeight, bIsWindowed, true);
	m_pTM->InitTextureManager(m_pD3D->GetDirect3DDevice(), m_pD3D->GetSprite());
	//m_pFM->InitFModManager(hWnd);
	m_pInput->Init(hWnd, hInstance);	
	m_pInput->AddDevice(new KeyboardDevice(m_pInput, true));
	m_pInput->AddDevice(new MouseDevice(m_pInput, false)); //We want exclusive access to mouse
	m_pInput->AddDevice(new XboxGamePadDevice(m_pInput, true, 1));

	//m_nTransitionImage = m_pTM->LoadTexture("resource/graphics/events.png");

	m_pInput->MapAction(ACT_ACCEPT,		DEV_KEYBOARD,	IT_ISPRESSED,	DIK_RETURN);
	m_pInput->MapAction(ACT_ACCEPT,		DEV_GAMEPAD,	IT_ISPRESSED,	XboxGamePadDevice::A_BUTTON);
	m_pInput->MapAction(ACT_BACK,		DEV_KEYBOARD,	IT_ISPRESSED,	DIK_ESCAPE);
	m_pInput->MapAction(ACT_BACK,		DEV_GAMEPAD,	IT_ISPRESSED,	XboxGamePadDevice::START);
	m_pInput->MapAction(ACT_MENUBACK,	DEV_KEYBOARD,	IT_ISPRESSED,	DIK_ESCAPE);
	m_pInput->MapAction(ACT_MENUBACK,	DEV_GAMEPAD,	IT_ISPRESSED,	XboxGamePadDevice::START);
	m_pInput->MapAction(ACT_MENUBACK,	DEV_GAMEPAD,	IT_ISPRESSED,	XboxGamePadDevice::B_BUTTON);
	m_pInput->MapAction(ACT_SKILLS,		DEV_GAMEPAD,	IT_ISPRESSED,	XboxGamePadDevice::BACK);
	m_pInput->MapAction(ACT_SKILLS,		DEV_KEYBOARD,	IT_ISPRESSED,	DIK_TAB);
	m_pInput->MapAction(ACT_MOVEDOWN,	DEV_KEYBOARD,	IT_ISPRESSED,	DIK_DOWN);
	m_pInput->MapAction(ACT_MOVEDOWN,	DEV_KEYBOARD,	IT_ISPRESSED,	DIK_S);
	m_pInput->MapAction(ACT_MOVEDOWN,	DEV_GAMEPAD,	IT_ISPRESSED,	XboxGamePadDevice::DPAD_DOWN);
	m_pInput->MapAction(ACT_MOVEUP,		DEV_KEYBOARD,	IT_ISPRESSED,	DIK_UP);
	m_pInput->MapAction(ACT_MOVEUP,		DEV_KEYBOARD,	IT_ISPRESSED,	DIK_W);
	m_pInput->MapAction(ACT_MOVEUP,		DEV_GAMEPAD,	IT_ISPRESSED,	XboxGamePadDevice::DPAD_UP);
	m_pInput->MapAction(ACT_MOVERIGHT,	DEV_KEYBOARD,	IT_ISPRESSED,	DIK_RIGHT);
	m_pInput->MapAction(ACT_MOVERIGHT,	DEV_KEYBOARD,	IT_ISPRESSED,	DIK_D);
	m_pInput->MapAction(ACT_MOVERIGHT,	DEV_GAMEPAD,	IT_ISPRESSED,	XboxGamePadDevice::DPAD_RIGHT);
	m_pInput->MapAction(ACT_MOVELEFT,	DEV_KEYBOARD,	IT_ISPRESSED,	DIK_LEFT);
	m_pInput->MapAction(ACT_MOVELEFT,	DEV_KEYBOARD,	IT_ISPRESSED,	DIK_A);
	m_pInput->MapAction(ACT_MOVELEFT,	DEV_GAMEPAD,	IT_ISPRESSED,	XboxGamePadDevice::DPAD_LEFT);
	m_pInput->MapAction(ACT_HOLDDOWN,	DEV_KEYBOARD,	IT_ISDOWN,		DIK_DOWN);
	m_pInput->MapAction(ACT_HOLDDOWN,	DEV_KEYBOARD,	IT_ISDOWN,		DIK_S);
	m_pInput->MapAction(ACT_HOLDUP,		DEV_KEYBOARD,	IT_ISDOWN,		DIK_UP);
	m_pInput->MapAction(ACT_HOLDUP,		DEV_KEYBOARD,	IT_ISDOWN,		DIK_W);
	m_pInput->MapAction(ACT_HOLDRIGHT,	DEV_KEYBOARD,	IT_ISDOWN,		DIK_RIGHT);
	m_pInput->MapAction(ACT_HOLDRIGHT,	DEV_KEYBOARD,	IT_ISDOWN,		DIK_D);
	m_pInput->MapAction(ACT_HOLDRIGHT,	DEV_GAMEPAD,	IT_ISDOWN,		XboxGamePadDevice::DPAD_RIGHT);
	m_pInput->MapAction(ACT_HOLDLEFT,	DEV_KEYBOARD,	IT_ISDOWN,		DIK_LEFT);
	m_pInput->MapAction(ACT_HOLDLEFT,	DEV_KEYBOARD,	IT_ISDOWN,		DIK_A);
	m_pInput->MapAction(ACT_HOLDLEFT,	DEV_GAMEPAD,	IT_ISDOWN,		XboxGamePadDevice::DPAD_LEFT);
	m_pInput->MapAction(ACT_LEFTCLICK,	DEV_MOUSE,		IT_ISPRESSED,	MouseDevice::LEFT);
	m_pInput->MapAction(ACT_LEFTMOUSEDOWN,	DEV_MOUSE,	IT_ISDOWN,		MouseDevice::LEFT);
	m_pInput->MapAction(ACT_LEFTMOUSEUP,	DEV_MOUSE,	IT_ISUP,		MouseDevice::LEFT);
	m_pInput->MapAction(ACT_RIGHTCLICK,	DEV_MOUSE,		IT_ISPRESSED,	MouseDevice::RIGHT);
	m_pInput->MapAction(ACT_ATTACK,		DEV_MOUSE,		IT_ISDOWN,		MouseDevice::LEFT);
	m_pInput->MapAction(ACT_ATTACK,		DEV_GAMEPAD,	IT_ISDOWN,		XboxGamePadDevice::A_BUTTON);
	m_pInput->MapAction(ACT_ATTACKED,	DEV_MOUSE,		IT_ISPRESSED,	MouseDevice::LEFT);
	m_pInput->MapAction(ACT_ATTACKED,	DEV_GAMEPAD,	IT_ISPRESSED,	XboxGamePadDevice::A_BUTTON);
	m_pInput->MapAction(ACT_MOVEX,		DEV_GAMEPAD,	IT_MOVEX,		XboxGamePadDevice::LEFT_THUMB);
	m_pInput->MapAction(ACT_MOVEY,		DEV_GAMEPAD,	IT_MOVEY,		XboxGamePadDevice::LEFT_THUMB);
	m_pInput->MapAction(ACT_ATTACKX,	DEV_GAMEPAD,	IT_MOVEX,		XboxGamePadDevice::RIGHT_THUMB);
	m_pInput->MapAction(ACT_ATTACKY,	DEV_GAMEPAD,	IT_MOVEY,		XboxGamePadDevice::RIGHT_THUMB);
	m_pInput->MapAction(ACT_SKILL1,		DEV_KEYBOARD,	IT_ISPRESSED,	DIK_1);
	m_pInput->MapAction(ACT_SKILL1,		DEV_GAMEPAD,	IT_ISPRESSED,	XboxGamePadDevice::X_BUTTON);
	m_pInput->MapAction(ACT_SKILL2,		DEV_KEYBOARD,	IT_ISPRESSED,	DIK_2);
	m_pInput->MapAction(ACT_SKILL2,		DEV_GAMEPAD,	IT_ISPRESSED,	XboxGamePadDevice::Y_BUTTON);
	m_pInput->MapAction(ACT_SKILL3,		DEV_KEYBOARD,	IT_ISDOWN,	DIK_3);
	m_pInput->MapAction(ACT_SKILL3,		DEV_GAMEPAD,	IT_ISDOWN,	XboxGamePadDevice::B_BUTTON);		
	//m_pAS->InitSystem("resource/NPC files/Player_Caster_Animations_test_New.nga");
	//m_pAS->InitSystem("resource/NPC files/test_Events.nga");

	m_pBMF  = new CBitmapFont("resource/fonts/dalelands.dat");

	int mouseImage = m_pTM->LoadTexture("resource/graphics/mouseCursor.png");

	m_pMouse = new CMouse(mouseImage, m_nScreenWidth, m_nScreenHeight);

   // PROTO: Test the setup of the Grid
   CGameplayState::GetInstance()->SetupGrid(CGameplayState::GetInstance()->HEX);

	CEventSystem::GetInstance()->RegisterClient("loadingready", this);
	CEventSystem::GetInstance()->RegisterClient("loadmainmenu", this);
	CEventSystem::GetInstance()->RegisterClient("rezchanged", this);

	
//#if _DEBUG
//	AddState(CMainMenuState::GetInstance());
//#else
	//AddState(CIntroState::GetInstance());
	//AddState(CMainMenuState::GetInstance());
	//AddState(CCreditsState::GetInstance());
	//AddState(COptionsState::GetInstance());
	//AddState(CTutorialState::GetInstance());
	//AddState(CMapSelectState::GetInstance());
	//AddState(CCharacterSelectState::GetInstance());
	////AddState(CLoadingState::GetInstance());

	//CIntroState::GetInstance()->SetHidden(false);
	//CMainMenuState::GetInstance()->SetHidden(true);
	//CCreditsState::GetInstance()->SetHidden(true);
	//COptionsState::GetInstance()->SetHidden(true);
	//CTutorialState::GetInstance()->SetHidden(true);
	//CCharacterSelectState::GetInstance()->SetHidden(true);
	//CMapSelectState::GetInstance()->SetHidden(true);
	////CLoadingState::GetInstance()->SetHidden(true);

	//ChangeState(CIntroState::GetInstance());
//#endif
	
	srand(unsigned int(time(0)));
}

//Clean Up
void CGame::Shutdown(void)
{

	if(m_pMouse)
	{
		m_pTM->UnloadTexture(m_pMouse->GetImageID());
		SAFE_RELEASE(m_pMouse);
	}
	if(m_pAS)
	{
		m_pAS->Shutdown();
		m_pAS = NULL;
	}
	if(m_pBMF)
	{
		delete m_pBMF;
		m_pBMF = NULL;
	}

	if(m_pGameStates.empty() == false)
	{
		while(m_pGameStates.empty() == false)
		{
			list<CGameState *>::reverse_iterator it = m_pGameStates.rbegin();
			(*it)->Exit();
			m_pGameStates.pop_back();
		}
	}
	if(m_pOM)
	{
		m_pOM->RemoveAllObjects();
		m_pOM->DeleteInstance();
		m_pOM = NULL;
	}
	if(m_pES)
	{
		m_pES->ShutdownEventSystem();
		m_pES = NULL;
	}
	if(m_pMS)
	{
		m_pMS->ShutdownMessageSystem();
		m_pMS = NULL;
	}
	//unaquire in the opposite order
	if(m_pInput)
	{
		m_pInput->Shutdown();
		m_pInput = NULL;
	}
	/*if(m_pFM)
	{
		m_pFM->ShutdownFModManager();
		m_pFM = NULL;
	}*/
	if(m_pTM)
	{
		m_pTM->ShutdownTextureManager();
		m_pTM = NULL;
	}
	if(m_pD3D)
	{
		m_pD3D->ShutdownDirect3D();
		m_pD3D = NULL;
	}
}

//Execution
bool CGame::Main(void)
{
	DWORD currentTime = GetTickCount();
	m_fDT = (float)(currentTime - m_dwPreviousTime) / 1000;

	m_dwPreviousTime = currentTime;

	//Input
	if(!Input())
		return false;
	//Update
	Update(m_fDT);
	//Draw
	Render(vector2D());

	return true;
}



bool CGame::Input(void)
{
	//Updates states of devices
	//m_pInput->AcquireAll();
	//m_pInput->Update();	//Call once a frame (usually)

	KeyboardDevice * keyboard = (KeyboardDevice*)CInput::GetInstance()->GetDevice(DEV_KEYBOARD);

	if(keyboard->KeyDown(DIK_RETURN) && (keyboard->KeyDown(DIK_LMENU) || keyboard->KeyDown(DIK_RMENU)))
	{
		m_bIsWindowed = !m_bIsWindowed;
		m_pD3D->ChangeDisplayParam(m_nScreenWidth, m_nScreenHeight, m_bIsWindowed);
		m_pInput->ClearDevices();
	}

	//if(m_pGameStates.empty() == false)
	//{
	//	list<CGameState *>::reverse_iterator it = m_pGameStates.rbegin();
	//	//if(!(*it)->GetHidden())
	//	{
	//		if((*it)->Input() == false)
	//			return false;
	//	}
	//}

	if(m_pGameStates.empty() == false)
	{
		list<CGameState*> statesToUpdate;

		for(list<CGameState *>::iterator it = m_pGameStates.begin(); it != m_pGameStates.end(); it++)
		{
			if((*it)->GetCurrentState() == SCRN_ACTIVE)
			{
				statesToUpdate.push_back(*it);
			}
		}

		while(statesToUpdate.empty() == false)
		{
			CGameState* state = statesToUpdate.back();
			statesToUpdate.pop_back();
			if(state->Input() == false)
				return false;
		}
	}

   // PROTO: Select the hex by marking it as drawing a different color
   if(m_pMouse->getClick_proto())
   {
      for(int i = 0; i < CGameplayState::GetInstance()->GetHexGridSize(); ++i)
      {
         HEX_SHAPE hex = CGameplayState::GetInstance()->GetGridLocationHEX(i).spotShape;
         
         PointInHexagon(m_pMouse->getClickX_proto(), m_pMouse->getClickY_proto(), hex);
      }
   }

	return true;
}

void CGame::Update(float dt)
{
	//if(CGameplayState::GetInstance()->GetLostFocus())
		//return;
	m_fFPSCounter += dt;

	if(m_fFPSCounter >= 1.0f)
	{
		m_fFPSCounter = 0.0f;
		m_nFPS = m_nFrameCounter;
		m_nFrameCounter = 0;
	}
	else
		m_nFrameCounter++;

	if(dt > 0.1f)
		dt = 0.1f;

	//m_pFM->Update();
	m_pInput->Update();
	m_pMouse->Update(dt, true);

	//Tell everyone the mouse has moved, this is mostly for 
	if(m_pMouse->ShowingMouse())
		if(m_pMouse->GetMouseMoved())
			m_pES->SendEvent("mousemoved");

	if(m_pGameStates.empty() == false)
	{
		
		int count = 0;

		list<CGameState*> statesToUpdate;

		for(list<CGameState *>::iterator it = m_pGameStates.begin(); it != m_pGameStates.end(); ++it)
		{
			statesToUpdate.push_back(*it);
			if((*it)->GetCurrentState() == SCRN_TRANSITION_OFF)
			{
				statesToUpdate.clear();
				statesToUpdate.push_back(*it);
				break;
			}
		}

		bool onTop = true;
		while(statesToUpdate.empty() == false)
		{
			CGameState * state = statesToUpdate.front();
			statesToUpdate.pop_front();
			if(state->GetCurrentState() != SCRN_HIDDEN)
			{
				state->Update(dt, onTop);
				onTop = false;
				if(state->GetCurrentState() == SCRN_TRANSITION_OFF)
					break;
			}
		}
	}

	m_pMS->ProcessMessages(dt);
	m_pES->ProcessEvents(dt);

}

void CGame::Render(vector2D & vCamera)
{
	m_pD3D->Clear(128, 128, 128);	
	m_pD3D->DeviceBegin();
	
	if(m_pGameStates.empty() == false)
	{
		list<CGameState*> statesToRender;

		for(list<CGameState *>::reverse_iterator it = m_pGameStates.rbegin(); it != m_pGameStates.rend(); it++)
		{
			if(!(*it)->GetHidden())
			{
				statesToRender.push_back(*it);
				if((*it)->GetCurrentState() == SCRN_TRANSITION_OFF)
				{
					statesToRender.clear();
					statesToRender.push_back(*it);
					break;
				}
			}
		}


		while(statesToRender.empty() == false)
		{
			CGameState * state = statesToRender.back();
			statesToRender.pop_back();

			m_pD3D->SpriteBegin();
			state->Render(vCamera);
			m_pD3D->SpriteEnd();
			if(state->GetCurrentState() == SCRN_TRANSITION_ON || state->GetCurrentState() == SCRN_TRANSITION_OFF)
			{
				m_pD3D->SpriteBegin();
				DWORD color = 0xFFFFFFFF;
				((BYTE*)&color)[3] = state->TransitionAlpha();
				vector2D size((float)m_nScreenWidth, (float)m_nScreenHeight);
				m_pTM->Draw(m_nTransitionImage, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0, &size, 0.0f, 0.0f, 0.0f, color);
				m_pD3D->SpriteEnd();

			}
		}
		
	}
   // Iterate through our vector and draw all items
   for (int i = 0; i < CGameplayState::GetInstance()->GetHexGridSize(); ++i)
   {
      HEX_SHAPE hex = CGameplayState::GetInstance()->GetGridLocationHEX(i).spotShape;
      CDirect3D::GetInstance()->DrawHex(vector2D(hex.centerX, hex.centerY), RED, hex.centerY - hex.top);
   }
	m_pD3D->SpriteBegin();

//#if _DEBUG
//	char buffer[255];
//	sprintf_s(buffer, 255, "FPS: %d", m_nFPS);
//	m_pBMF->DrawText(buffer, 0, 0);
//#endif
	

	//We render the mouse in a seperate block because
	//we dont' want to render using zSorting with other
	//objects
	//Render the mouse
	if(m_pMouse->ShowingMouse())
		m_pMouse->Render(vector2D());


	m_pD3D->SpriteEnd();
	m_pD3D->DeviceEnd();
	m_pD3D->Present();
}

void CGame::AddState(CGameState * state)
{
	m_pGameStates.push_back(state);
	state->Enter();
}

void CGame::RemoveState(CGameState * state)
{
	if(state != NULL)
	{
		state->Exit();
		m_pGameStates.remove(state);
	}
}

void CGame::ChangeState(CGameState * state)
{
	/*if(m_pGameStates.empty() == false)
	{
		for(list<CGameState *>::iterator it = m_pGameStates.begin(); it != m_pGameStates.end(); it++)
		{
			if(!(*it)->GetHidden())
				(*it)->SetHidden(true);
		}
	}

	state->SetHidden(false);*/

	RemoveAllStates();
	AddState(state);
	state->SetHidden(false);
}

void CGame::RemoveAllStates()
{
	while(m_pGameStates.empty() == false)
	{
		(*m_pGameStates.rbegin())->Exit();
		m_pGameStates.pop_back();
	}
}


void CGame::SetSFXVolume(float volume)
{
	if(volume < 0.0f)
		volume = 0.0f;

	if(volume > 1.0f)
		volume = 1.0f;
	m_fSFXVolume = volume;
}
float  CGame::GetSFXVolume() const
{
	return m_fSFXVolume;
}

float  CGame::GetGlobalVolume() const
{
	return m_fGlobalVolume;
}

void CGame::SetMusicVolume(float volume)
{
	if(volume < 0.0f)
		volume = 0.0f;
	else if(volume > 1.0f)
		volume = 1.0f;
	m_fMusicVolume = volume;
}
float  CGame::GetMusicVolume() const
{
	return m_fMusicVolume;
}

void CGame::SetSoundPanning(float panning)
{
	if(panning < -100)
		panning = -100;

	if(panning > 100)
		panning = 100;

	m_fPanning = panning;
}
float  CGame::GetSoundPanning() const
{
	return m_fPanning;
}
void CGame::SetGlobalScaleX()
{
	if(m_nPrevScrW != GetScreenWidth())
	{
		m_nPrevScrW = GetScreenWidth();
		switch(m_nPrevScrW)
		{
		case 800:
			m_fGlobalScaleX = 0.79f;
			break;
		case 1024:
			m_fGlobalScaleX = 1;
			break;
		case 1152:
			m_fGlobalScaleX = 1.15f;
			break;
		case 1280:
			m_fGlobalScaleX = 1.28f;
			break;
		case 1400:
			m_fGlobalScaleX = 1.4f;
			break;
		case 1440:
			m_fGlobalScaleX = 1.4f;
			break;
		case 1600:
			m_fGlobalScaleX = 1.6f;
			break;
		};
	}
}
void CGame::SetGlobalScaleY()
{
	if(m_nPrevScrH != GetScreenHeight())
	{
		m_nPrevScrH = GetScreenHeight();
		switch(m_nPrevScrH)
		{
		case 600:
			m_fGlobalScaleY = 0.79f;
			break;
		case 768:
			m_fGlobalScaleY = 1;
			break;
		case 864:
			m_fGlobalScaleY = 1.15f;
			break;
		case 720:
			m_fGlobalScaleY = 0.95f;
			break;
		case 800:
			m_fGlobalScaleY = 1.1f;
			break;
		case 960:
			m_fGlobalScaleY = 1.2f;
			break;
		case 1024:
			m_fGlobalScaleY = 1.2f;
			break;
		case 1050:
			m_fGlobalScaleY = 1.2f;
			break;
		case 900:
			m_fGlobalScaleY = 1.18f;
			break;
		};
	}
}

bool CGame::HandleEvent(CEvent* pEvent)
{
	//if(pEvent->GetEventID() == "loadingready")
	//{
	//	ChangeState(CGameplayState::GetInstance());
	//	AddState(CPauseState::GetInstance());
	//	CPauseState::GetInstance()->SetHidden(true);
	//	AddState(COptionsState::GetInstance());
	//	COptionsState::GetInstance()->SetHidden(true);
	//	AddState(CSkillState::GetInstance());
	//	CSkillState::GetInstance()->SetHidden(true);
	//	AddState(CSaveLoadState::GetInstance());
	//	CSaveLoadState::GetInstance()->SetHidden(true);
	//	return false;
	//}
	//if(pEvent->GetEventID() == "loadmainmenu")
	//{
	//	ChangeState(CMainMenuState::GetInstance());
	//	AddState(CCreditsState::GetInstance());
	//	AddState(COptionsState::GetInstance());
	//	AddState(CTutorialState::GetInstance());
	//	AddState(CMapSelectState::GetInstance());
	//	AddState(CCharacterSelectState::GetInstance());
	//	//AddState(CLoadingState::GetInstance());

	//	CCreditsState::GetInstance()->SetHidden(true);
	//	COptionsState::GetInstance()->SetHidden(true);
	//	CTutorialState::GetInstance()->SetHidden(true);
	//	CCharacterSelectState::GetInstance()->SetHidden(true);
	//	CMapSelectState::GetInstance()->SetHidden(true);
	//	return false;
	//}
	if(pEvent->GetEventID() == "rezchanged")
	{
		for(list<CGameState*>::iterator state = m_pGameStates.begin(); state != m_pGameStates.end(); ++state)
		{
			(*state)->SetUIPositions();
		}
	}
	return true;
}

const CGameState * CGame::GetTopState()
{
	if(m_pGameStates.empty() == false)
	{
		for(list<CGameState *>::reverse_iterator it = m_pGameStates.rbegin(); it != m_pGameStates.rend(); it++)
		{
			if(!(*it)->GetHidden())
			{
				return (*it);
			}
		}
	}
	else
		return NULL;

	return NULL;
}